//logs.js
const util = require('../../utils/util.js')
var tools = require('../../js/tools.js')

var app = getApp()

Page({
  data: {
    loadingstate: false,
    logs: [],
    capacity: -1111.01,
    leader: 0,
    showModal: false,
    group: 0,
    task: 0,
    admin: 0,
    group_num: 0,
    task_num: 0,
    admin_num: 0,

    circleList: [],//圆点数组

    awardList: [],//奖品数组

    colorCircleFirst: '#FFDF2F',//圆点颜色1

    colorCircleSecond: '#FE4D32',//圆点颜色2

    colorAwardDefault: '#F5F0FC',//奖品默认颜色

    colorAwardSelect: '#ffe400',//奖品选中颜色

    indexSelect: 0,//被选中的奖品index

    presentIndex: 46, //控制中奖的奖品代码

    isRunning: false,//是否正在抽奖

    imageAward: [

      '../../images/1.jpg',

      '../../images/2.jpg',

      '../../images/3.jpg',

      '../../images/4.jpg',

      '../../images/5.jpg',

      '../../images/6.jpg',

      '../../images/7.jpg',

      '../../images/8.jpg',

    ],//奖品图片数组
  },

  coutNum: function (e) {
    if (e > 1000 && e < 10000) {
      e = (e / 1000).toFixed(1) + 'k'
    }
    if (e > 10000) {
      e = (e / 10000).toFixed(1) + 'W'
    }
    return e
  },
  btn: function () {
    let that = this;
    wx.getStorage({
      key: 'Landingtime',
      success(res) {
        // console.log("登录时间信息存在，正在检查是否过期..")
        // tools.gettimestamp()
        var date = tools.convertDateFromString(res.data)
        if (tools.comparetime(date) == 0) {
          //已经过期
          wx.showToast({
            title: '请先点击"我的"里的头像登录',
            icon: 'none',
            duration: 3000
          })
        }
        else{
          that.setData({
            showModal: true
          })
        }
      },
      fail(res) {//此时为第一次登陆
        wx.showToast({
          title: '请先点击"我的"里的头像登录',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  // testcent: function () {
  //   wx.navigateTo({
  //     url: '../../pages/grouplist/grouplist',
  //     success: function (res) {
  //       console.log(res)
  //     },
  //     fail:function(res){
  //       console.log(res)
  //     }
  //   })
  // },

  // 禁止屏幕滚动
  preventTouchMove: function () {
  },

  // 弹出层里面的弹窗
  ok: function () {
    this.setData({
      showModal: false
    })
  },


  onShow: function () {
    let that = this;
    wx.getStorage({
      key: 'pwdWithSalt',
      success(res) {
        wx.request({
          url: 'https://localhost:8080/page_log_getinfo',
          data: {
            pwdWithSalt: res.data
          },
          success(res) {
            if (res.data.errCode == 2019) {
              wx.showToast({
                title: '数据请求失败,请检查网络',
                icon: 'none',
                image: '',
                duration: 1500,
                mask: false,
              });
            }
            else if (res.data.errCode == 2020) {
              that.setData({
                'group_num': res.data.group_num,
                'task_num': res.data.task_num,
                'admin_num': res.data.admin_num
              })
            }
          }
        })
      }
    })
    wx.showLoading({
      title: '数据加载中',
      mask: true,
    })
    let i = 0;
    numDH();
    function numDH() {
      if (i < 20) {
        setTimeout(function () {
          that.setData({
            group: i,
            task: i,
            admin: i
          })
          i++
          numDH();
        }, 20)
      } else {
        that.setData({
          group: that.coutNum(that.data.group_num),
          task: that.coutNum(that.data.task_num),
          admin: that.coutNum(that.data.admin_num)
        })
      }
    }
    wx.hideLoading()
  },

  onLoad: function () {

    var _this = this;

    //圆点设置

    var leftCircle = 7.5;

    var topCircle = 7.5;

    var circleList = [];

    for (var i = 0; i < 24; i++) {

      if (i == 0) {

        topCircle = 15;

        leftCircle = 15;

      } else if (i < 6) {

        topCircle = 7.5;

        leftCircle = leftCircle + 102.5;

      } else if (i == 6) {

        topCircle = 15

        leftCircle = 620;

      } else if (i < 12) {

        topCircle = topCircle + 94;

        leftCircle = 620;

      } else if (i == 12) {

        topCircle = 565;

        leftCircle = 620;

      } else if (i < 18) {

        topCircle = 570;

        leftCircle = leftCircle - 102.5;

      } else if (i == 18) {

        topCircle = 565;

        leftCircle = 15;

      } else if (i < 24) {

        topCircle = topCircle - 94;

        leftCircle = 7.5;

      } else {

        return

      }

      circleList.push({ topCircle: topCircle, leftCircle: leftCircle });

    }

    this.setData({

      circleList: circleList

    })



    //圆点闪烁

    setInterval(function () {

      if (_this.data.colorCircleFirst == '#FFDF2F') {

        _this.setData({

          colorCircleFirst: '#FE4D32',

          colorCircleSecond: '#FFDF2F',

        })

      } else {

        _this.setData({

          colorCircleFirst: '#FFDF2F',

          colorCircleSecond: '#FE4D32',

        })

      }

    }, 500)//设置圆点闪烁的效果



    //奖品item设置

    var awardList = [];

    //间距,怎么顺眼怎么设置吧.

    var topAward = 25;

    var leftAward = 25;

    for (var j = 0; j < 8; j++) {

      if (j == 0) {

        topAward = 25;

        leftAward = 25;

      } else if (j < 3) {

        topAward = topAward;

        //166.6666是宽.15是间距.下同

        leftAward = leftAward + 166.6666 + 15;

      } else if (j < 5) {

        leftAward = leftAward;

        //150是高,15是间距,下同

        topAward = topAward + 150 + 15;

      } else if (j < 7) {

        leftAward = leftAward - 166.6666 - 15;

        topAward = topAward;

      } else if (j < 8) {

        leftAward = leftAward;

        topAward = topAward - 150 - 15;

      }

      var imageAward = this.data.imageAward[j];

      awardList.push({ topAward: topAward, leftAward: leftAward, imageAward: imageAward });

    }

    this.setData({

      awardList: awardList

    })

  },



  //开始抽奖

  startGame: function () {

    if (this.data.isRunning) return

    this.setData({

      isRunning: true

    })

    var _this = this;

    var indexSelect = 0

    var i = 0;

    var timer = setInterval(function () {

      indexSelect++;

      //这里我只是简单粗暴用y=30*x+200函数做的处理.可根据自己的需求改变转盘速度和结果

      i += _this.data.presentIndex;

      if (i > 1000) {

        //去除循环

        clearInterval(timer)

        //获奖提示

        wx.getStorage({
          key: 'pwdWithSalt',
          success(res) {
            wx.request({
              url: 'https://localhost:8080/getspace',
              data: {
                pwdWithSalt: res.data
              },
              success(res) {
                wx.showModal({

                  title: '恭喜您',

                  content: '获得了' + res.data + "KB空间",

                  showCancel: false,//去掉取消按钮

                  success: function (res) {

                    if (res.confirm) {

                      _this.setData({

                        isRunning: false

                      })

                    }

                  }

                })
              }
            })
          }
        })



      }

      indexSelect = indexSelect % 8;

      _this.setData({

        indexSelect: indexSelect

      })

    }, (200 + i))

  },


  Bpersonal_info: function () {
    wx.getStorage({
      key: 'Landingtime',
      success(res) {
        // console.log("登录时间信息存在，正在检查是否过期..")
        // tools.gettimestamp()
        var date = tools.convertDateFromString(res.data)
        if (tools.comparetime(date) == 0) {
          //已经过期
          wx.showToast({
            title: '请先点击"我的"里的头像登录',
            icon: 'none',
            duration: 3000
          })
        }
        else{
          wx.navigateTo({
            url: "../../pages/Personal_info/Personal_info",
            success: function (res) {
            }
          })
        }
      },
      fail(res) {//此时为第一次登陆
        wx.showToast({
          title: '请先点击"我的"里的头像登录',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  onShareAppMessage: function (res) {
    if (res.from === 'button') {
    }
    return {
      title: '给你推荐一款班委收作业神器',
      path: '/pages/index/index',
      imageUrl: '../../pages/index/shou.jpg',
      // success: (res) => {
      //   console.log("转发成功", res);
      // },
      // fail: (res) => {
      //   console.log("转发失败", res);
      // }
      //该接口目前已作废
    }
  },

  denglu:function(){
    console.log('登录')
    wx.getStorage({
      key: 'Landingtime',
      success(res) {
        // console.log("登录时间信息存在，正在检查是否过期..")
        // tools.gettimestamp()
        var date = tools.convertDateFromString(res.data)
        if (tools.comparetime(date) == 0) {
          //已经过期
          wx.navigateTo({
            url: '../../pages/landing/landing',
            success: function (res) {
              console.log(res)
            },
            fail: function (res) {
              wx.showToast({
                title: '调用登录页面失败',
                icon: 'none',
                duration: 1000
              })
            }
          })
        }
        else {
          wx.clearStorage()
          wx.navigateTo({
            url: '../../pages/landing/landing',
            success: function (res) {
              console.log(res)
            },
            fail: function (res) {
              wx.showToast({
                title: '调用登录页面失败',
                icon: 'none',
                duration: 1000
              })
            }
          })
        }
      },
      fail(res) {//此时为第一次登陆
        wx.navigateTo({
          url: '../../pages/landing/landing',
          success: function (res) {
            console.log(res)
          },
          fail: function (res) {
            wx.showToast({
              title: '调用登陆失败',
              icon: 'loading',
              duration: 1000
            })
          }
        })
      }
    }),
      wx.getStorage({
        key: 'pwdWithSalt',
        success(res) {
          console.log("盐存在")
        },
        fail(res) {//此时为第一次登陆
          wx.navigateTo({
            url: '../../pages/landing/landing',
            success: function (res) {
              console.log(res)
            },
            fail: function (res) {
              wx.showToast({
                title: '调用登陆失败',
                icon: 'loading',
                duration: 1000
              })
            }
          })
        }
      })
  },

  RichScan: function () {
    // wx.navigateTo({
    //   url:'admin'
    // })

    wx.setClipboardData({
      data: 'https://admin.qingyoudeng.club:4488',
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '网址复制成功'
            })
          }
        })
      }
    })

    // wx.scanCode({
    //   success(res) {
    //     var string = res.result;
    //     var str = string.split(',')
    //     var key = str[0]
    //     console.log(str)
    //     wx.getStorage({
    //       key: 'pwdWithSalt',
    //       success(res) {
    //         wx.request({
    //           url: 'https://localhost:8080/loadToWeb',
    //           data: {
    //             key: key,
    //             pwdWithSalt: res.data
    //           },
    //           success(res) {
    //             if (res.data == 1) {
    //               wx.navigateTo({
    //                 url: 'RichScanSuccess/RichScanSuccess'
    //               })
    //               console.log('??')
    //             }
    //             else {
    //               wx.showToast({
    //                 title: '发生异常,请检查网络',
    //                 icon: 'none'
    //               })
    //             }
    //           }
    //         })
    //       }
    //     })

    //   },
    // })
  },

  Bclear:function(){
    wx.showModal({
      title:'确认清空缓存',
      content:'说明:1.登录信息会丢失\n2.若遇到无法登录可以使用此功能',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.clearStorage({
            success(){
              wx.showToast({
                title: '删除成功',
                icon: 'none'
              });
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
  // Bgroup_info: function () {
  //   wx.navigateTo({
  //     url: "../../pages/Group_info/Group_info",
  //     success: function (res) {
  //     }
  //   })
  // },

  // Bgroup_numbr: function () {
  //   wx.navigateTo({
  //     url: "../../pages/Group_numbr/Group_numbr",
  //     success: function (res) {
  //     }
  //   })
  // },

  // Bbecome_leader:function(){
  //   wx.navigateTo({
  //     url: '../../pages/become_leader/Bbecome_leader',
  //   });

  // }
})
